package com.xfc.school.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfc.common.constant.Constants;
import com.xfc.common.utils.DateUtils;
import com.xfc.school.domain.ScCourseList;
import com.xfc.school.domain.ScCourseSchedule;
import com.xfc.school.mapper.ScCourseMapper;
import com.xfc.school.mapper.ScCourseScheduleMapper;
import com.xfc.school.service.IScCourseScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 课程日期Service业务层处理
 * 
 * @author wtf
 * @date 2021-12-26
 */
@Slf4j
@Service
public class ScCourseScheduleServiceImpl extends ServiceImpl<ScCourseScheduleMapper, ScCourseSchedule> implements IScCourseScheduleService{

    @Autowired
    private ScCourseScheduleMapper courseScheduleMapper;


    @Async
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCourseList(ScCourseList courseList,Integer type) {
        log.info("课程自动生成开始:{}",DateUtil.now());
            //获取当前时间
            String today= DateUtil.today();
            //转化时分秒(开始时间)
            String startTime = DateUtil.formatTime(courseList.getStartTime());
            //转化时分秒(结束时间)
            String endTime = DateUtil.formatTime(courseList.getEndTime());
            //获取需要加减的天数
            int week= DateUtils.getWeek(courseList.getWeek());
            //获取当前课程年月日时分秒
            Date nowStartTime= DateUtil.parse(today+" "+startTime);
            Date nowEndTime=DateUtil.parse(today+" "+endTime);
            //计算课程表时间
            Date nowStartTimeCourse = DateUtil.offset(nowStartTime, DateField.DAY_OF_MONTH, week);
            Date nowEndTimeCourse = DateUtil.offset(nowEndTime, DateField.DAY_OF_MONTH, week);
            //存储课程
//            ScCourseSchedule ScCourseSchedule1=new ScCourseSchedule();
//            ScCourseSchedule1.setCourseState(0);
//            ScCourseSchedule1.setCourseNumber(courseList.getClassId());
//            ScCourseSchedule1.setTeacherNumber(courseList.getTeacherId());
//            ScCourseSchedule1.setCreateTime(new Date());
//            ScCourseSchedule1.setStartTime(nowStartTimeCourse);
//            ScCourseSchedule1.setEndTime(nowEndTimeCourse);
//            ScCourseSchedule1.setCourseListId(courseList.getId());
//            //添加原始课程
//            courseScheduleMapper.insert(ScCourseSchedule1);
            //生成一个月课程
            for (int i=1;i<=Constants.COURSE_DAY;i++){
                ScCourseSchedule ScCourseSchedule2=new ScCourseSchedule();
                ScCourseSchedule2.setCourseState(0);
                ScCourseSchedule2.setCourseNumber(courseList.getClassId());
                ScCourseSchedule2.setTeacherNumber(courseList.getTeacherId());
                ScCourseSchedule2.setCreateTime(new Date());
                ScCourseSchedule2.setStartTime(DateUtil.offsetWeek(nowStartTimeCourse,i));
                ScCourseSchedule2.setEndTime(DateUtil.offsetWeek(nowEndTimeCourse,i));
                ScCourseSchedule2.setCourseListId(courseList.getId());
                courseScheduleMapper.insert(ScCourseSchedule2);
            }
            if (type==1) {
                //生成过去一年的课程
                for (int i = Constants.COURSE_AGO_DAY; i <= 0; i++) {
                    ScCourseSchedule ScCourseSchedule2 = new ScCourseSchedule();
                    ScCourseSchedule2.setCourseState(0);
                    ScCourseSchedule2.setCourseNumber(courseList.getClassId());
                    ScCourseSchedule2.setTeacherNumber(courseList.getTeacherId());
                    ScCourseSchedule2.setCreateTime(new Date());
                    ScCourseSchedule2.setStartTime(DateUtil.offsetWeek(nowStartTimeCourse, i));
                    ScCourseSchedule2.setEndTime(DateUtil.offsetWeek(nowEndTimeCourse, i));
                    ScCourseSchedule2.setCourseListId(courseList.getId());
                    courseScheduleMapper.insert(ScCourseSchedule2);
                }
            }

        log.info("课程自动生成结束:{}",DateUtil.now());

    }

}
